//
//  newYiYuanDuoBaoViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/9/20.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "newYiYuanDuoBaoViewController.h"
#import "Macros.h"
#import "HttpRequest.h"
#import "newYiYuanDuoBaoTableViewCell.h"
#import "newYiYuanDuoBao.h"
#import "UIImageView+WebCache.h"
#import "yiYuanDuoBao.h"
#import "goodsDetailViewController.h"
#import "yiYuanDuoBaoNextView.h"
#import "TCLYUser.h"
#import "goodsDetail.h"

#import "SVProgressHUD.h"

@interface newYiYuanDuoBaoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *headerView01;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property(nonatomic,retain)newYiYuanDuoBaoTableViewCell *myCell;
@property(nonatomic,copy)NSMutableArray *dataArray;
@property(nonatomic,assign)float pinkView_width;
//通过msg的字段判断是不是新人可以参加新人一元夺宝
@property(nonatomic,copy)NSString *msg;
@end

@implementation newYiYuanDuoBaoViewController
@synthesize dataArray,pinkView_width;
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setBackGroundColor];
    dataArray=[NSMutableArray array];
    self.myTableView.delegate=self;
    self.myTableView.dataSource=self;
    [self getDataFromNet];
    [SVProgressHUD showWithStatus:@"加载中请稍后..."];
    self.myTableView.hidden=YES;
}
//设置背景色
-(void)setBackGroundColor
{
    self.headerView.backgroundColor=[UIColor colorWithRed:244/255.0 green:142/255.0 blue:177/255.0 alpha:1];
    self.headerView01.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1];
//    int pinkView_width00=self.myCell.pinkView.frame.size.width;
//    NSLog(@"pinkView_width00=%d",pinkView_width00);

  
}
//从服务器获取数据
-(void)getDataFromNet
{
    NSString*urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/indiana/activeindiana"];
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"uid"]=[NSString stringWithFormat:@"%@",userID];
    //    parameters[@"user_id"]=@"1";
    [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
//         NSLog(@"json=%@",json);
         dataArray=[json objectForKey:@"data"];
//         self.msg=(int)[json objectForKey:@"msg"];
         self.msg=[json objectForKey:@"msg"];
         //200是成功，400是不成功
//         NSLog(@"self.msg=%@",self.msg);
//         NSLog(@"json=%@",json);
         if (dataArray.count>0)
         {
             [self dismiss];
//             [self.myTableView reloadData];
         }
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
- (void)dismiss
{
    self.myTableView.hidden=NO;
    [SVProgressHUD dismiss];
    [self.myTableView reloadData];
}
//-(void)setTableSource
//{
//
//
//
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    newYiYuanDuoBao *nyyd=[newYiYuanDuoBao shardeManager];
    static NSString *str=@"cell";
   self.myCell=[tableView dequeueReusableCellWithIdentifier:str];
    if (self.myCell==nil)
    {
        self.myCell=[[[NSBundle mainBundle] loadNibNamed:@"newYiYuanDuoBaoTableViewCell" owner:self options:nil]lastObject];
        nyyd.nameString=[dataArray[indexPath.row] objectForKey:@"name"];
        nyyd.picString=[dataArray[indexPath.row] objectForKey:@"pic"];
        nyyd.involeString=[dataArray[indexPath.row] objectForKey:@"invole"];
        nyyd.sumString=[dataArray[indexPath.row] objectForKey:@"sum"];
        nyyd.idString=[dataArray[indexPath.row] objectForKey:@"id"];
        int idNum=[nyyd.idString intValue];
        self.myCell.joinButton.tag=idNum;
        float pinkView_width0=self.myCell.grayView.frame.size.width;
//        NSLog(@"pinkView_width0=%f",pinkView_width0);
        float involve=[nyyd.involeString intValue];
        float sum=[nyyd.sumString intValue];
        float value=involve/sum;
//        float pinkView_x=self.myCell.grayView.frame.origin.x;
//        float pinkView_y=self.myCell.grayView.frame.origin.y;
//        NSLog(@"pinkView_x=%f",pinkView_x);
//        NSLog(@"pinkView_y=%f",pinkView_y);
//        NSLog(@"value=%f",value);
        if (value==0)
        {
            pinkView_width=0;
        }else
        {
            pinkView_width=value*pinkView_width0;
        }
//        NSLog(@"pinkView_width=%f",pinkView_width);
//        float a_x=self.myCell.nameLabel.frame.origin.x;
        self.myCell.pinkView.backgroundColor=pinkColor;
        float width000=self.myCell.pinkView.frame.size.width;
        float xx=self.myCell.pinkView.frame.origin.x;
        float yy=self.myCell.pinkView.frame.origin.y;
        
        self.myCell.pinkView.frame=CGRectMake(xx, yy, 0, 4);
        self.myCell.pinkView.hidden=YES;
//        NSLog(@"width000=%f",width000);
//        NSLog(@"xx=%f",xx);
//        NSLog(@"yy=%f",yy);
//        NSLog(@"a_x=%f",a_x);
        UIView *pinkView=[[UIView alloc]initWithFrame:CGRectMake(xx, yy, width000*value, 4)];
        pinkView.backgroundColor=pinkColor;
        [self.myCell.contentView addSubview:pinkView];
        
        [self.myCell.joinButton addTarget:self action:@selector(joinTo:) forControlEvents:UIControlEventTouchUpInside];
        self.myCell.nameLabel.text=nyyd.nameString;
        self.myCell.nowPeopleNumLable.text=nyyd.involeString;
        self.myCell.sumNumLabel.text=nyyd.sumString;
        //设置按钮的圆角半径不会被遮挡
        [self.myCell.joinButton.layer setMasksToBounds:YES];
        //        [commitBtn.layer setCornerRadius:10];
        //设置边界的宽度
        [self.myCell.joinButton.layer setBorderWidth:1];
        [self.myCell.joinButton.layer setBorderColor:pinkColor.CGColor];
        [self.myCell.joinButton setTitleColor:pinkColor forState:UIControlStateNormal];
        self.myCell.nowPeople.textColor=pinkColor;
        self.myCell.nowPeopleNumLable.textColor=pinkColor;
        self.myCell.sumLabel.textColor=pinkColor;
        self.myCell.sumNumLabel.textColor=pinkColor;
        //点击无颜色
        self.myCell.selectionStyle=UITableViewCellSelectionStyleNone;

    }
    NSURL* imagePath= [NSURL URLWithString:nyyd.picString];
    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
    [self.myCell.picImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"banner1"]];
    return self.myCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    return 125.0f;
}
- (IBAction)goBack:(id)sender
{
//    if (self.msg==200)
//    {
        [self.navigationController popToRootViewControllerAnimated:YES];
//    }
//    else
//    {
//        UIAlertController * alertController=[UIAlertController alertControllerWithTitle:nil message:@"您已经参与过免费夺宝" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *okAction=[UIAlertAction actionWithTitle:@"谢谢" style:UIAlertActionStyleDefault handler:nil];
//        [alertController addAction:okAction];
//        [self presentViewController:alertController animated:YES completion:nil];
//    }
}
-(void)joinTo:(UIButton*)sender
{
//    NSLog(@"12312");
    yiYuanDuoBao *yydb=[yiYuanDuoBao shardeManager];
    yydb.idString=[NSString stringWithFormat:@"%ld",sender.tag];
    yiYuanDuoBaoNextView *yydbn=[yiYuanDuoBaoNextView shardeManager];
    yydbn.whichViewToNext=3;
    if (yydb.idString)
    {
//        NSLog(@"self.msg=%@",self.msg);
        int num=[self.msg intValue];
        if (num==200)
        {
            goodsDetail *gd=[goodsDetail shardeManager];
            gd.num=0;
            goodsDetailViewController *good=[goodsDetailViewController new];
            [self.navigationController pushViewController:good animated:YES];
        }
        else if(num==400)
        {
        UIAlertController * alertController=[UIAlertController alertControllerWithTitle:nil message:@"您已经参与过免费夺宝" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction=[UIAlertAction actionWithTitle:@"谢谢" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"谢谢");
        }];
        [alertController addAction:okAction];
        [self presentViewController:alertController animated:YES completion:nil];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
